// @ts-check
var util = require('./ossutil');
var fs = require('fs');
const COS = require('cos-nodejs-sdk-v5');

const cos = new COS({
    // 必选参数
    SecretId: "AKID85mCLDpAE1YomTu4xZtxkFTQ6VdV6QSE",
    SecretKey: "WmkRr0sTosfBSTDfqkNjoc9OM2SbT4PT",
});

export default {

    // Bucket: 'pr-user-1257745182', /* 填入您自己的存储桶，必须字段 */
    // Region: 'ap-shanghai',  /* 存储桶所在地

    uploadFile(filepath) {
        console.log('fs：' + fs.stat);
        var filename = '3mb.zip';
        cos.uploadFile({
            Bucket: 'pr-user-1257745182',
            Region: 'ap-shanghai',
            Key: filename,
            FilePath: filepath,
            SliceSize: 1024 * 1024 * 5, // 大于5mb才进行分块上传
            onProgress: function (info) {
                var percent = Math.floor(info.percent * 10000) / 100;
                var speed = Math.floor(info.speed / 1024 / 1024 * 100) / 100;
                console.log('进度：' + percent + '%; 速度：' + speed + 'Mb/s;');
            },
        }, function (err, data) {
            console.log('上传' + (err ? '失败' : '完成'));
            console.log(err || data);
            fs.unlinkSync(filepath);
        });
    },

    uploadFiles(filepath) {
        util.createFile(filepath, 1024 * 1024 * 10, function (err) {
            var filename = 'mb.zip';
            cos.uploadFiles({
                files: [{
                    Bucket: 'pr-user-1257745182',
                    Region: 'ap-shanghai',
                    Key: '1' + filename,
                    FilePath: filepath,
                }, {
                    Bucket: 'pr-user-1257745182',
                    Region: 'ap-shanghai',
                    Key: '2' + filename,
                    FilePath: filepath,
                }],
                SliceSize: 1024 * 1024,
                onProgress: function (info) {
                    var percent = Math.floor(info.percent * 10000) / 100;
                    var speed = Math.floor(info.speed / 1024 / 1024 * 100) / 100;
                    console.log('进度：' + percent + '%; 速度：' + speed + 'Mb/s;');
                },
                onFileFinish: function (err, data, options) {
                    console.log(options.Key + ' 上传' + (err ? '失败' : '完成'));
                },
            }, function (err, data) {
                console.log(err || data);
                fs.unlinkSync(filepath);
            });
        });
    }


}


